package com.wz.dhs.config.idwork;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;

import javax.annotation.PostConstruct;
import java.util.HashMap;

/**
 * @author xyb
 * @Description 分布式ID序列化器
 * @since 14:09 2022/7/15
 */
@Configuration
@Slf4j
public class IdContextHolder {

    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    public static final String APP_NAME = "gyp";

    public static SnowflakeIdWorker snowflakeIdWorker;

    public static Long nextId() {
        return snowflakeIdWorker.nextId();
    }


    @PostConstruct
    public void initData() {
        HashMap<String, Integer> workMap = IdWorkerConfig.calculateDataIdAndWorkId2(APP_NAME,redisConnectionFactory);
        Integer workId = workMap.get("workId");
        Integer dataId = workMap.get("dataId");
        log.info("应用{}加载workId:{}和datacenterId成功:{}", APP_NAME, workId, dataId);
        snowflakeIdWorker = new SnowflakeIdWorker(workId, dataId);
    }


}
